<template> 
  <div class="app-container">
    <!--隐藏或展开筛选条件-->
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>筛选条件</span>
      <el-button
        class="btn-add"
        @click="changeFilterVisible()"
        size="mini">
        <div v-if="filterVisible">隐藏</div>
        <div v-else>展开</div>
      </el-button>
    </el-card>

     <!--筛选条件-->
    <el-card v-show="filterVisible" class="filter-container" shadow="never" >
      <el-row>
            <el-col :span="18"><i class="el-icon-search"></i><span>筛选搜索</span></el-col>
            <el-col :span="6">
              <el-button
                style="float: right"
                @click="handleSearchList()"
                type="primary"
                size="mini">
                查询结果
              </el-button>
             
              <el-button
                style="float: right;margin-right: 15px"
                @click="handleResetSearch()"
                size="mini">
                重置
              </el-button>
            </el-col>
      </el-row>
       <el-row>
          <el-col :span="24">
           <el-form :inline="true" label-position="right" :model="listQuery" :rules="listQueryRule" ref="listQueryRef" size="mini" label-width="140px">
              <el-form-item label="学科："  prop="subjectId" >
                <el-select v-model="listQuery.subjectId" placeholder="请选择学科" >
                  <el-option
                    v-for="item in subjectOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="卷子状态：" prop="examStatus">
                <el-select v-model="listQuery.status" placeholder="请选择试卷状态" clearable>
                <el-option
                    v-for="item in examStatusOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value" >
                  </el-option>
                 </el-select>
              </el-form-item>
              <el-form-item label="卷子名称：" size="small" prop="examName" >
                <el-input  v-model="listQuery.name"  placeholder="请输入"></el-input>
              </el-form-item>
               <el-form-item label="卷子说明：" size="small" prop="examComment" >
                <el-input  v-model="listQuery.comment"  placeholder="请输入"></el-input>
              </el-form-item>
             

            </el-form>
          </el-col>
      </el-row>
    </el-card>
    
    <div class="table-container">
      <el-table ref="productTable"
                :data="list"
                style="width: 100%"
                @selection-change="handleSelectionChange"
                v-loading="listLoading"
                border>
        <el-table-column type="selection" width="60" align="center"></el-table-column>
        <el-table-column label="No" width="100" align="center">
          <template slot-scope="scope">{{scope.row.id}}</template>
        </el-table-column>
        <el-table-column label="试卷名字" width="120" align="center">
          <template slot-scope="scope">{{scope.row.name}}</template>
        </el-table-column>
        <el-table-column label="试卷说明" width="320" align="center">
          <template slot-scope="scope">{{scope.row.comment}}</template>
        </el-table-column>
        
        <el-table-column label="试卷状态" width="140" align="center">
          <template slot-scope="scope">{{scope.row.status| optionFilter(examStatusOptions)}}</template>
        </el-table-column>
          <el-table-column label="操作" width="160" align="center">
          <template slot-scope="scope">
            <p>
              

              <el-button 
                size="mini" type="primary"
                @click="handleViewExam(scope.$index, scope.row)">查看
              </el-button>
             
            </p>
            <p>
              
              <el-button 
                size="mini"
                
                @click="handleDelete(scope.$index, scope.row)">删除
              </el-button>
            </p>
          </template>
        </el-table-column>
      </el-table>
    </div>
    
    
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[10,20,30]"
        :current-page.sync="listQuery.pageNum"
        :total="total">
      </el-pagination>
    </div>
    


    
  </div>
</template>
<script>
  import { mapGetters } from 'vuex'
  import { deleteExam,fetchList } from '@/api/exam';
  
  import {deepClone} from '@/utils/helper';

  const defaultListQuery = {
   
    pageNum: 1,
    pageSize: 10,
    
    subjectId: null,    //学科Id
    status:null,
    name:null,
    comment:null

  };
  export default {
    name: "examList",
    data() {
       

    
      return {
       
        listQuery: Object.assign({}, defaultListQuery),
        list: null,
        total: null,
        listLoading: false,
       

        //筛选框是否显示
        filterVisible:true,
        //自定义listQueryForm的校验规则

        listQueryRule :{
           subjectId: [
             {required: true, message:"请输入卷子学科",trigger: ['blur','change']},
          ],
        },

       
       
        
       
      }
    },
    computed:{
      ...mapGetters([
          'name',
          'subjectOptions',
          'examStatusOptions',
          'userid'
        ]),
      
    },
   
    filters: {
      optionFilter(value,options) {
        let res= options.find(function(x) {
          return x.value ===value;
          }
        )
        if (res){
          return res.label
        }else{
          return ''
        }
      },
    },
 
    methods: {
      handleResetSearch(){
        this.listQuery=Object.assign({}, defaultListQuery)
        

      },
      
      handleViewExam(index,row){
        let routeData = this.$router.resolve({path:'/ems/examView',query:{id:row.id,name:row.name,status:row.status,questionIds:row.question_ids,comment:row.comment}});
        window.open(routeData.href, '_blank');

        //this.$router.push({path:'/qms/questionUpdate',query:{id:row.id}});
      },
     
     
      handleDelete(index,row){
        
        this.$confirm('再次确认删除该试卷？', '提示', {
                  confirmButtonText: '确定',
                  cancelButtonText: '取消',
                  type: 'warning'
                }).then(() => {
                  deleteExam(row.id).then(response=>{

                    this.$message({
                          type: 'success',
                          message: '删除成功',
                          duration:1000
                      });

                    this.handleSearchList()

                  
                }).catch(error => {

                      this.$message({
                          type: 'error',
                          message: '删除失败',
                          duration:1000
                      });
              
                  });


                }).catch(() => {
                      
              
                 });

      },

      changeFilterVisible(){
          this.filterVisible=!this.filterVisible
      },
     


     


      //获得试题列表
      getList() {
        

              this.listLoading = true;

             
              var submitParam=deepClone(this.listQuery)
              
              
              submitParam.userId=this.userid
              

              fetchList(submitParam).then(response => {
                this.listLoading = false;
                this.list = response.data.list;
                this.total = response.data.total;
              });
          
    },

     


      //点击“查询结果按钮”
      handleSearchList() {
        this.$refs.listQueryRef.validate(valid => {
          if (valid) {
            this.listQuery.pageNum = 1;
            this.getList();
          }
        })
      },
     
      handleSizeChange(val) {
        this.listQuery.pageNum = 1;
        this.listQuery.pageSize = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.pageNum = val;
        this.getList();
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      
     
    },
   
  }
</script>



